package com.ecarx.vhaltests.hy11.testcases.climate;

import android.car.VehicleAreaSeat;
import android.car.VehicleAreaType;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.util.Log;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.xui.adaptapi.automotive.VehicleArea;
import com.ecarx.xui.adaptapi.car.IWrapper;
import com.ecarx.xui.adaptapi.car.hvac.IHvac;
import com.ecarx.xui.adaptapi.car.property.IProperty;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import java.util.logging.Logger;

import androidx.test.platform.app.InstrumentationRegistry;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HvacFuncOneKeyVentilationTest extends VhalUnitTestBase {
    private static final String TAG = "HvacFuncOneKeyVentilationTest";
    private static boolean halValue;
    private static int halStatus;
    private static int halArea;

    private static int HVAC_FUNC_ONE_KEY_VENTILATION;

    private static final int SERVICE_ID_CLIMATE = 112;
    private static final int OP_ID_CLIMATE = 0xc8;

    static final Object lock = new Object();
    static IpcpClient ipcpClient = new IpcpClient();

    protected static IWrapper wrapper;

    @BeforeClass
    public static void testInit() {
        wrapper = com.ecarx.xui.adaptapi.car.Car.createWrapper(InstrumentationRegistry.getInstrumentation().getTargetContext());
        HVAC_FUNC_ONE_KEY_VENTILATION = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, IProperty.HVAC_FUNC_ONE_KEY_VENTILATION).getPropertyId();
        Log.d(TAG, "HVAC_FUNC_ONE_KEY_VENTILATION hal id ：" + HVAC_FUNC_ONE_KEY_VENTILATION);
        mCarPropertyMgr.registerCallback(carPropertyEventCallback, HVAC_FUNC_ONE_KEY_VENTILATION, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        setConfig(OP_ID_CLIMATE);
    }

    @Test
    public void test01_HvacFuncOneKeyVentilationAvail() {
        setHvacFuncOneKeyVentilationAvail(1, 0);
        hvacFuncOneKeyVentilationAvailTest(true);
    }

    @Test
    public void test02_HvacFuncOneKeyVentilationNotAvail() {
        setHvacFuncOneKeyVentilationAvail(3, 0);
        hvacFuncOneKeyVentilationAvailTest(false);
    }

    @Test
    public void test03_HvacFuncOneKeyVentilationSetOn() {
        setHvacFuncOneKeyVentilationAvail(1, 0);
        hvacFuncOneKeyVentilationSet(true);
    }

    @Test
    public void test04_HvacFuncOneKeyVentilationSetOff() {
        setHvacFuncOneKeyVentilationAvail(1, 1);
        hvacFuncOneKeyVentilationSet(false);
    }

    private static CarPropertyManager.CarPropertyEventCallback carPropertyEventCallback = new CarPropertyManager.CarPropertyEventCallback() {
        @Override
        public void onChangeEvent(CarPropertyValue carPropertyValue) {
            try {
                halValue = (boolean) carPropertyValue.getValue();
                halStatus = carPropertyValue.getStatus();
                halArea = carPropertyValue.getAreaId();
                Log.d(TAG, "HVAC_FUNC_ONE_KEY_VENTILATION onChangeEvent value: propertyId ：" + carPropertyValue.getPropertyId() + " propertyValue：" + carPropertyValue.getValue() + "  status : " + carPropertyValue.getStatus());
                synchronized (lock) {
                    lock.notify();
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }

        @Override
        public void onErrorEvent(int i, int i1) {
        }
    };

    public void lockWait() {
        try {
            synchronized (lock) {
                lock.wait(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void hvacFuncOneKeyVentilationAvailTest(boolean value) {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(HVAC_FUNC_ONE_KEY_VENTILATION, VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL);
        Log.d(TAG, "HvacFuncOneKeyVentilationAvailTest  isAvail：" + isAvail);
        Assert.assertEquals(value, isAvail);
    }

    private void hvacFuncOneKeyVentilationSet(boolean value) {
        boolean bRet = false;
        Log.d(TAG, "HvacFuncOneKeyVentilationSet set HVAC_FUNC_ONE_KEY_VENTILATION value ：" + value);
        mCarPropertyMgr.setBooleanProperty(HVAC_FUNC_ONE_KEY_VENTILATION, VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL, value);
        delay();
        bRet = mCarPropertyMgr.getBooleanProperty(HVAC_FUNC_ONE_KEY_VENTILATION, VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL);
        Assert.assertEquals(value, bRet);
        lockWait();
        Assert.assertEquals(value, halValue);
    }


    private void setHvacFuncOneKeyVentilationAvail(int avail, int onOff) {
        Log.d(TAG, "setHvacFuncOneKeyVentilationAvail avail：" + avail);
        IpcpClient ipcpClient = new IpcpClient();
        String[] config_indexs = {"paclventilation.availability", "paclventilation.data"};
        int[] datas = {avail, onOff};
        boolean bb = ipcpClient.setClimaSignal(SERVICE_ID_CLIMATE, OP_ID_CLIMATE, config_indexs, datas);
        Assert.assertTrue(bb);
        lockWait();
    }

    private static void setConfig(int OperId) {
        int climateOperId[] = {OperId};
        ipcpClient.setConfig(SERVICE_ID_CLIMATE, climateOperId);
    }

}
